#include "monitor.h"
#include "ui_monitor.h"

#include <QDebug>
#include "cirbuff.h"

Monitor::Monitor(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Monitor)
{
    ui->setupUi(this);

    m_tempTimer = new QTimer(this);
    m_humiTimer = new QTimer(this);
    m_tipsTimer = new QTimer(this);
    connect(m_tempTimer, SIGNAL(timeout()),
            this, SLOT(slotTimeOutTemp()));
    connect(m_humiTimer, SIGNAL(timeout()),
            this, SLOT(slotTimeOutHumi()));
    connect(m_tipsTimer, SIGNAL(timeout()),
            this, SLOT(slotTimeOutTips()));

    m_protocol.clearProtocoldata();
    m_serial = new SerialThread;
    connect(m_serial, SIGNAL(signalWarnInfo(int)),
            this, SLOT(slotWarnInfo(int)));
    connect(m_serial, SIGNAL(signalModeInfo(int)),
            this, SLOT(slotModeInfo(int)));
    connect(m_serial, SIGNAL(signalBeepStatu(bool)),
            this, SLOT(slotBeepStatu(bool)));
    connect(m_serial, SIGNAL(signalTempValue(int,QString)),
            this, SLOT(slotTempValue(int,QString)));
    connect(m_serial, SIGNAL(signalHumiValue(int,QString)),
            this, SLOT(slotHumiValue(int,QString)));
    connect(m_serial, SIGNAL(signalShowRecvData(QByteArray)),
            this, SLOT(slotShowRecvData(QByteArray)));

    ui->te_recvText->append(tr("检测到端口列表:"));
    ui->te_recvText->append((tr("/dev/ttyAMA0")));
    ui->cb_comPort->addItem("/dev/ttyAMA0");


    ui->cb_baudRate->setCurrentIndex(7);//默认115200
    ui->pb_openSerial->setText(tr("打开串口"));
    ui->pb_send->setEnabled(false);

    on_pb_openSerial_clicked();
}

Monitor::~Monitor()
{
    delete ui;
}
void Monitor::slotShowRecvData(QByteArray ba)
{
    ui->te_recvText->append(QString::fromLatin1(ba));
}

void Monitor::on_pb_send_clicked()
{
    m_serial->sendDataToSerial(ui->le_sendText->text().toLatin1());
}

void Monitor::on_pb_openSerial_clicked()
{
    if(ui->pb_openSerial->text()==tr("打开串口"))
    {
        if(m_serial->openSerialPort())
        {
            qDebug() << "openSerialPort";
            m_serial->start();
            ui->cb_comPort->setEnabled(false);
            ui->cb_baudRate->setEnabled(false);
            ui->cb_crcTest->setEnabled(false);
            ui->cb_dataBit->setEnabled(false);
            ui->cb_stopBit->setEnabled(false);
            ui->lb_statuFlag->setStyleSheet("background-color:red");
            ui->pb_openSerial->setText(tr("关闭串口"));
            ui->pb_send->setEnabled(true);
        }else
        {
        }
    }else
    {
        m_serial->closeSerialPort();

        ui->cb_comPort->setEnabled(true);
        ui->cb_baudRate->setEnabled(true);
        ui->cb_crcTest->setEnabled(true);
        ui->cb_dataBit->setEnabled(true);
        ui->lb_statuFlag->setStyleSheet("background-color:rgb(130,130,130)");
        ui->pb_openSerial->setText(tr("打开串口"));
        ui->pb_send->setEnabled(false);
    }
}

void Monitor::on_pb_clearRecvWidget_clicked()
{
    ui->te_recvText->clear();
}

void Monitor::on_pb_beep_clicked()
{
    m_protocol.clearProtocoldata();
    if(ui->pb_beep->text() == "Beep ON")
    {
        QByteArray array = m_protocol.packetMsg(DEV_Beep, CMD_BeepOn);
        m_serial->sendDataToSerial(array);
    }else if(ui->pb_beep->text() == "Beep OFF")
    {
        QByteArray array = m_protocol.packetMsg(DEV_Beep, CMD_BeepOff);
        m_serial->sendDataToSerial(array);
    }
}

void Monitor::slotWarnInfo(int flag)
{
    switch(flag)
    {
    case CMD_WarnData: ui->lb_warn->setText(QString("警告: 数据正常")); break;
    case CMD_WarnOvrT: ui->lb_warn->setText(QString("警告: 温度过高"));  break;
    case CMD_WarnLowT: ui->lb_warn->setText(QString("警告: 温度过低"));  break;
    case CMD_WarnOvrH: ui->lb_warn->setText(QString("警告: 湿度过高"));  break;
    case CMD_WarnLowH: ui->lb_warn->setText(QString("警告: 湿度过低"));  break;
    }
}

void Monitor::slotModeInfo(int flag)
{
    switch(flag)
    {
    case 0: ui->lb_mode->setText(QString("模式: 实时模式")); break;
    case 1: ui->lb_mode->setText(QString("模式: 温度模式")); break;
    case 2: ui->lb_mode->setText(QString("模式: 湿度模式")); break;
    }
}

void Monitor::slotBeepStatu(bool on)
{
    if(on)
    {
        ui->lb_beep->setStyleSheet("background-color:red");
        ui->pb_beep->setText("Beep OFF");
    }else
    {
        ui->lb_beep->setStyleSheet("background-color:rgb(130,130,130)");
        ui->pb_beep->setText("Beep ON");
    }
}

void Monitor::slotTimeOutTemp(void)
{
    QByteArray array = m_protocol.packetMsg(DEV_Temp, CMD_TempReal);
    m_serial->sendDataToSerial(array);
    array = m_protocol.packetMsg(DEV_Temp, CMD_TempMaxV);
    m_serial->sendDataToSerial(array);
    array = m_protocol.packetMsg(DEV_Temp, CMD_TempMinV);
    m_serial->sendDataToSerial(array);
}

void Monitor::slotTempValue(int flag, QString value)
{
    if(flag == CMD_TempReal)
    {
        ui->lcd_tempV->display(value);
    }else if(flag == CMD_TempMaxV)
    {
        ui->lcd_tempMaxV->display(value);
    }else if(flag == CMD_TempMinV)
    {
        ui->lcd_tempMinV->display(value);
    }
}

void Monitor::slotHumiValue(int flag, QString value)
{
    if(flag == CMD_HumiReal)
    {
        ui->lcd_humiV->display(value);
    }else if(flag == CMD_HumiMaxV)
    {
        ui->lcd_humiMaxV->display(value);
    }else if(flag == CMD_HumiMinV)
    {
        ui->lcd_humiMinV->display(value);
    }
}

void Monitor::slotTimeOutHumi(void)
{
    QByteArray array = m_protocol.packetMsg(DEV_Humi, CMD_HumiReal);
    m_serial->sendDataToSerial(array);
    array = m_protocol.packetMsg(DEV_Humi, CMD_HumiMaxV);
    m_serial->sendDataToSerial(array);
    array = m_protocol.packetMsg(DEV_Humi, CMD_HumiMinV);
    m_serial->sendDataToSerial(array);
}

void Monitor::on_pb_tempStart_clicked()
{
    if(m_tempTimer->isActive())
    {
        m_tempTimer->stop();
        ui->pb_tempStart->setText("开启");
    }else
    {
        m_tempTimer->start(ui->sb_tempInterval->value()*1000);
        ui->pb_tempStart->setText("停止");
    }
}

void Monitor::on_pb_humiStart_clicked()
{
    if(m_humiTimer->isActive())
    {
        m_humiTimer->stop();
        ui->pb_humiStart->setText("开启");
    }else
    {
        m_humiTimer->start(ui->sb_humiInterval->value()*1000);
        ui->pb_humiStart->setText("停止");
    }
}

void Monitor::slotTimeOutTips(void)
{
    QByteArray array = m_protocol.packetMsg(DEV_Warn, CMD_WarnGain);
    m_serial->sendDataToSerial(array);
    array = m_protocol.packetMsg(DEV_Mode, CMD_ModeGain);
    m_serial->sendDataToSerial(array);
}

void Monitor::on_pb_tipStart_clicked()
{
    if(m_tipsTimer->isActive())
    {
        m_tipsTimer->stop();
        ui->pb_tipStart->setText("开启");
    }else
    {
        m_tipsTimer->start(ui->sb_tipInterval->value()*1000);
        ui->pb_tipStart->setText("停止");
    }
}

void Monitor::on_pb_tempMaxVAdd_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Temp, CMD_TempMaxA);
    m_serial->sendDataToSerial(array);
}

void Monitor::on_pb_tempMaxVSub_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Temp, CMD_TempMaxS);
    m_serial->sendDataToSerial(array);
}

void Monitor::on_pb_tempMinVAdd_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Temp, CMD_TempMinA);
    m_serial->sendDataToSerial(array);
}

void Monitor::on_pb_tempMinVSub_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Temp, CMD_TempMinS);
    m_serial->sendDataToSerial(array);
}

void Monitor::on_pb_humiMaxVAdd_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Humi, CMD_HumiMaxA);
    m_serial->sendDataToSerial(array);
}

void Monitor::on_pb_humiMaxVSub_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Humi, CMD_HumiMaxS);
    m_serial->sendDataToSerial(array);
}

void Monitor::on_pb_humiMinVAdd_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Humi, CMD_HumiMinA);
    m_serial->sendDataToSerial(array);
}

void Monitor::on_pb_humiMinVSub_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Humi, CMD_HumiMinS);
    m_serial->sendDataToSerial(array);
}

void Monitor::on_cb_mode_currentIndexChanged(int index)
{
    QByteArray array;
    if(index == 0)
    {
        array = m_protocol.packetMsg(DEV_Mode, CMD_ModeData);
    }else if(index == 1)
    {
        array = m_protocol.packetMsg(DEV_Mode, CMD_ModeTemp);
    }else if(index == 2)
    {
        array = m_protocol.packetMsg(DEV_Mode, CMD_ModeHumi);
    }

    m_serial->sendDataToSerial(array);
}
